package com.iqik.work.controller;


import com.iqik.work.pojo.User;
import com.iqik.work.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/demo")
    @ResponseBody
    public String demo() {

        return "demo http request";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        return "login";
    }

    @RequestMapping(value = "/toLogin", method = RequestMethod.GET)
    public String login(@RequestParam("name") String name, @RequestParam("password") String password, HttpSession session) {
        User user = userService.login(name, password);
        session.setAttribute("USER", user);
        return "result";
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
